package cn.tedu.v1_1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *  聊天室 Version1.1  【写软件没有顺序---只有业务逻辑】
 *  功能：服务端启动【申请：端口号】---客户端链接服务器【IP+端口】--写出
 *  完成哪些功能    新增什么功能：
 */
public class Server {
    private ServerSocket serverSocket;
    public Server(){
        try {
            serverSocket=new ServerSocket(6666);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void start(){
        InputStream is=null;
        OutputStream os=null;
        InputStreamReader isr=null;
        OutputStreamWriter osw=null;
        BufferedReader br=null;
        BufferedWriter bw=null;
        PrintWriter pw=null;

        try {
            System.out.println("---等待客户端连接---");
            Socket socket= null;//卡住--等待客户端发起的连接操作
            socket = serverSocket.accept();
            System.out.println("一个客户端连接成功了");

            is = socket.getInputStream();
            os=socket.getOutputStream();

            isr = new InputStreamReader(is, "UTF8");
            osw=new OutputStreamWriter(os,"UTF8");

            br = new BufferedReader(isr);
            bw=new BufferedWriter(osw);
            pw=new PrintWriter(bw);


//            String line=null;
//            while(true){
//                line=bw.newLine();
//                pw.println(line);
//                System.out.println(line);
//            }


            String str = null;
            while (true) {
                str = br.readLine();

                System.out.println("客户端说的话：" + str);

            }
        } catch(FileNotFoundException e){
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        finally {
            try {
                if (br!=null)
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //4.
    public static void main(String[] args)  {

        Server server=new Server();
        server.start();
    }
}
